package com.xyy.saas.payment.controller;

import com.xyy.saas.payment.api.impl.PaymentMerchantApiImpl;
import com.xyy.saas.payment.api.impl.PaymentOrderApiImpl;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.AccountChannelEnum;
import com.xyy.saas.payment.cores.param.BankQueryParam;
import com.xyy.saas.payment.cores.param.RegionQueryParam;
import com.xyy.saas.payment.cores.param.SubBankQueryParam;
import com.xyy.saas.payment.cores.vo.*;
import com.xyy.saas.payment.merchant.core.service.UserInfoService2;
import com.xyy.saas.payment.merchant.core.vo.UserInfoVo;
import com.xyy.saas.payment.settle.core.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping(value = {"/api/yop"})
public class YopTestController {
    @Resource
    private PaymentOrderApiImpl paymentOrderApi;
    @Resource
    private PaymentMerchantApiImpl paymentMerchantApi;
    @Autowired
    private AccountService accountService;
    @Resource
    private UserInfoService2 userInfoService2;

    /**
     * 查询行政地址
     */
    @RequestMapping(value = "/regionQuery", method = RequestMethod.POST)
    public Object regionQuery(@RequestBody RegionQueryParam param) {
        return paymentMerchantApi.regionQuery(param);
    }

    /**
     * 查询银行
     */
    @RequestMapping(value = "/bankQuery", method = RequestMethod.POST)
    public Object bankQuery(@RequestBody BankQueryParam param) {
        return paymentMerchantApi.bankQueryV2(param);
    }

    /**
     * 查询支行
     */
    @RequestMapping(value = "/subBankQuery", method = RequestMethod.POST)
    public Object subBankQuery(@RequestBody SubBankQueryParam param) {
        return paymentMerchantApi.subBankQueryV2(param);
    }

    /**
     * 查询开户信息
     */
    @RequestMapping(value = "/queryUserInfo", method = RequestMethod.POST)
    public Object queryUserInfo(String accountId) throws Exception {
        long start = System.currentTimeMillis();
        UserInfoVo userInfoVo = userInfoService2.queryUserInfo(accountId);
        log.info("查询开户信息耗时：{}", System.currentTimeMillis() - start);
        return userInfoVo;
    }

    /**
     * 企业开户
     */
    @RequestMapping(value = "/createMerchant", method = RequestMethod.POST)
    public Object createMerchant(@RequestBody MerchantAccountCreateVo param) {
        return paymentMerchantApi.merchantCertifyAccountCreate(param, StringUtils.EMPTY);
    }

    /**
     * 个人开户
     */
    @RequestMapping(value = "/createPersonal", method = RequestMethod.POST)
    public Object createPersonal(@RequestBody PersonalAccountCreateVo userDto) throws PaymentException {
        return paymentMerchantApi.personalCertifyAccountCreate(userDto, StringUtils.EMPTY);
    }

    /**
     * 企业换绑
     */
    @RequestMapping(value = "/merchantSettleCardReBind", method = RequestMethod.POST)
    public Object merchantSettleCardReBind(@RequestBody MerchantSettleCardBindVo param) {
        return paymentMerchantApi.merchantSettleCardReBind(param, StringUtils.EMPTY);
    }

    /**
     * 个人换绑
     */
    @RequestMapping(value = "/personalSettleCardReBind", method = RequestMethod.POST)
    public Object personalSettleCardReBind(@RequestBody PersonalSettleCardBindVo param) {
        return paymentMerchantApi.personalSettleCardReBind(param, StringUtils.EMPTY);
    }

    /**
     * 查询账户余额
     */
    @RequestMapping(value = "/queryAccount", method = RequestMethod.POST)
    public Object queryAccount(String userId, String businessIdType, String balanceAccountType) {
        return accountService.queryAccount(userId, businessIdType, AccountChannelEnum.AGGREGATE.getChannel(), balanceAccountType);
    }

    /**
     * 转账
     */
    @RequestMapping(value = "/separateAccountFromUser", method = RequestMethod.POST)
    public Object separateAccountFromUser(@RequestBody SeparateAccountFromUserVo separateAccountFromUserVo) {
        return paymentOrderApi.separateAccountFromUser(separateAccountFromUserVo, StringUtils.EMPTY);
    }

    /**
     * 提现
     */
    @RequestMapping(value = "/withdraw", method = RequestMethod.POST)
    public Object withdraw(@RequestBody OrderWithdrawVo orderWithdrawVo) throws PaymentException {
        return paymentOrderApi.withdraw(orderWithdrawVo, StringUtils.EMPTY);
    }
}
